/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2024/9/7
 * Time: 15:32
 */

#include <gtest/gtest.h>
#include "DataHandle.h"
#include <QtWidgets>
#include <QtSql>

class QtAppEnvironment : public ::testing::Environment {
public:
    // 构造函数中初始化 QApplication
    QtAppEnvironment() {
        static int argc = 0;
        static char* argv[] = { nullptr };
        app = new QApplication(argc, argv);
    }

    // 析构函数中销毁 QApplication
    ~QtAppEnvironment() override {
        delete app;
    }

private:
    QApplication* app = nullptr;
};

TEST(DataHandleUser, MySqlDriversSupportTest)
{
    QStringList drivers = QSqlDatabase::drivers();
    EXPECT_TRUE(drivers.contains("QMYSQL"));

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    EXPECT_TRUE(db.isValid());
}

TEST(DataHandleUser, DataHandleAddUserTest)
{
    DataHandle handle;
    auto info = std::make_shared<UserInfo>();
    info->id = "10000";
    info->name = "测试用户名";
    EXPECT_TRUE(handle.AddUser(info));

    EXPECT_TRUE(!handle.AddUser(info));
}

TEST(DataHandleUser, DataHandleFindUserTest)
{
    DataHandle handle;

    auto user = handle.FindUser("10000");
    EXPECT_TRUE(nullptr != user);
    EXPECT_EQ("10000",user->id);
    EXPECT_EQ("测试用户名",user->name);

    user = handle.FindUser("10001");
    EXPECT_TRUE(nullptr == user);
}

TEST(DataHandleUser, DataHandleRemoveUserTest)
{
    DataHandle handle;

    EXPECT_TRUE(handle.RemoveUser("10000"));
    EXPECT_TRUE(!handle.RemoveUser("10001"));
}

TEST(DataHandleGroup,DataHandleAddGroupTest)
{
    DataHandle handle;

    auto info = std::make_shared<GroupInfo>();
    info->id = "20000";
    info->name = "测试用户组";
    EXPECT_TRUE(handle.AddGroup(info));

    EXPECT_TRUE(!handle.AddGroup(info));

    info->id = "20001";
    info->name = "测试用户组1";
    EXPECT_TRUE(handle.AddGroup(info));
}

TEST(DataHandleGroup,DataHandleFindGroupWithNameLikeTest)
{
    DataHandle handle;

    auto group = handle.FindGroupWithNameLike("用户");
    EXPECT_TRUE(group.size() == 2);

    EXPECT_EQ("20000",group[0]->id);
    EXPECT_EQ("20001",group[1]->id);

    EXPECT_EQ("测试用户组",group[0]->name);
    EXPECT_EQ("测试用户组1",group[1]->name);

    group = handle.FindGroupWithNameLike("10001");
    EXPECT_TRUE(group.empty());
}

TEST(DataHandleGroup,DataHandleFindGroupWithIDLikeTest)
{
    DataHandle handle;

    auto group = handle.FindGroupWithIDLike("200");
    EXPECT_TRUE(group.size() == 2);

    EXPECT_EQ("20000",group[0]->id);
    EXPECT_EQ("20001",group[1]->id);

    EXPECT_EQ("测试用户组",group[0]->name);
    EXPECT_EQ("测试用户组1",group[1]->name);

    group = handle.FindGroupWithIDLike("300");
    EXPECT_TRUE(group.empty());
}

TEST(DataHandleGroup,DataHandleRemoveGroupTest)
{
    DataHandle handle;

    EXPECT_TRUE(handle.RemoveGroup("20000"));
    EXPECT_TRUE(handle.RemoveGroup("20001"));
    EXPECT_TRUE(!handle.RemoveUser("20000"));
}

TEST(DataHandleGroupAction,DataHandleJoinGroupTest)
{
    DataHandle handle;

    {
        auto info = std::make_shared<GroupInfo>();
        info->id = "20000";
        info->name = "测试用户组";
        EXPECT_TRUE(handle.AddGroup(info));
    }
    {
        auto info = std::make_shared<UserInfo>();
        info->id = "10000";
        info->name = "测试用户名";
        EXPECT_TRUE(handle.AddUser(info));
    }
    EXPECT_TRUE(handle.JoinGroup("20000","10000"));
}


TEST(DataHandleGroupAction,DataHandleMemberIsExistTest)
{
    DataHandle handle;

    EXPECT_TRUE(handle.MemberIsExist("20000","10000"));
    EXPECT_TRUE(!handle.MemberIsExist("20000","10001"));
}

TEST(DataHandleGroupAction,DataHandleMembersTest)
{
    DataHandle handle;
    {
        auto info = std::make_shared<UserInfo>();
        info->id = "10002";
        info->name = "测试用户名1";
        EXPECT_TRUE(handle.AddUser(info));

         EXPECT_TRUE(handle.JoinGroup("20000","10002"));
    }

    auto members = handle.Members("20000");
    EXPECT_TRUE(members.size() == 2);

    EXPECT_EQ("10000",members[0]->id);
    EXPECT_EQ("10002",members[1]->id);

    EXPECT_EQ("测试用户名",members[0]->name);
    EXPECT_EQ("测试用户名1",members[1]->name);
}

TEST(DataHandleGroupAction,DataHandleUserTypeTest)
{
    DataHandle handle;

    auto type = handle.UserType("20000","10000");
    EXPECT_EQ(type,GroupInfo::Type::Normal);
}

TEST(DataHandleGroupAction,DataHandleModifyGroupMemberRoleTest)
{
    DataHandle handle;

    EXPECT_TRUE(handle.ModifyGroupMemberRole("20000","10000",GroupInfo::Type::Admin));

    auto type = handle.UserType("20000","10000");
    EXPECT_EQ(type,GroupInfo::Type::Admin);

    EXPECT_TRUE(!handle.ModifyGroupMemberRole("20001","10001",GroupInfo::Type::Admin));

    type = handle.UserType("20001","10001");
    EXPECT_EQ(type,GroupInfo::Type::None);
}

TEST(DataHandleGroupAction,DataHandleExitGroupTest)
{
    DataHandle handle;

    EXPECT_TRUE(handle.ExitGroup("20000","10000"));
    EXPECT_TRUE(!handle.ExitGroup("20000","10000"));

    {
        EXPECT_TRUE(handle.RemoveUser("10000"));
        EXPECT_TRUE(handle.RemoveUser("10002"));
        EXPECT_TRUE(handle.RemoveGroup("20000"));
    }
}

int main(int argc, char **argv) {
    ::testing::AddGlobalTestEnvironment(new QtAppEnvironment);  // 注册环境
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}